#!/bin/sh -e

export LDFLAGS="$LDFLAGS -static"

mk() {
    make \
        TOPDIR=.. \
        libdir=/usr/lib/ \
        bindir=/usr/bin/ \
        mandir=/usr/share/man/ \
        includedir=/usr/include/ \
        "$@"
}

(
    cd src
    clsed '/#include <sys\/cdefs.h>/d' compiler.h

    # Build static targets.
    mk libefiboot.a libefivar.a efiboot.pc efivar.pc
    patch -p2 < ../musl-compat.patch
    mk efivar-static

    # Install the binary.
    clinst -Dm755 efivar-static "$1/usr/bin/efivar"
    
    # Install libraries.
    clinst -Dm644 libefiboot.a  "$1/usr/lib/libefiboot.a"
    clinst -Dm644 libefivar.a   "$1/usr/lib/libefivar.a"
    
    # Install pkgconf files.
    clinst -Dm644 efiboot.pc    "$1/usr/lib/pkgconfig/efiboot.pc"
    clinst -Dm644 efivar.pc     "$1/usr/lib/pkgconfig/efivar.pc"
    
    # Install headers.
    find include -type f | while read -r header; do
        clinst -Dm644 "$header" "$1/usr/$header"
    done
)


mk -C docs DESTDIR="$1" install
